import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ky_tool_plugin/config/ky_project_configure.dart';
import 'dart:ui';
import 'package:ky_tool_plugin/sizefit/ky_int_extension.dart';
import 'package:ky_tool_plugin/sizefit/ky_double_extension.dart';
import 'package:ky_tool_plugin/tools/ky_log_utils.dart';


class KYCommonSiftPage extends Dialog {
  List <dynamic>dataList;
  bool backMiss;
  //用于区分同一方法调用多次该页面
  int siftType;
  int initItem;
  String titleStr;
  final chooseCallBack;
  final cancelCallBack;
  Color? buttonColor;
  KYCommonSiftPage({required this.dataList,required this.titleStr, required this.backMiss,required this.siftType,this.chooseCallBack,this.cancelCallBack,required this.initItem,this.buttonColor = KYNavBlackColor});


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return KYCommonSiftPageWidget(dataList: this.dataList,backMiss: this.backMiss,siftType: this.siftType,chooseCallBack: this.chooseCallBack,
      cancelCallBack: this.cancelCallBack,initItem: this.initItem,titleStr: this.titleStr,buttonColor: this.buttonColor,);
  }

}

class KYCommonSiftPageWidget extends StatefulWidget{

  List <dynamic>dataList;
  bool backMiss;
  int siftType;
  int initItem;
  String titleStr;
  final chooseCallBack;
  final cancelCallBack;
  Color? buttonColor;
  KYCommonSiftPageWidget({required this.dataList, required this.backMiss,required this.siftType,this.chooseCallBack,this.cancelCallBack,required this.initItem,required this.titleStr,this.buttonColor = KYNavBlackColor});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return KYCommonSiftPageWidgetState();
  }

}

class KYCommonSiftPageWidgetState extends State<KYCommonSiftPageWidget>{

  int _selectedIndex = 0;

  double _widgetHeight = 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _selectedIndex = widget.initItem;

    Future.delayed(Duration.zero,(){
      _widgetHeight = 320.px+MediaQuery.of(context).padding.bottom;
      setState(() {

      });
    });


  }


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: (){
        if(widget.backMiss){

          _widgetHeight = 1;
          setState(() {

          });
          Future.delayed(Duration(milliseconds: 200)).then((value){
            Navigator.of(context).pop();
            widget.cancelCallBack(widget.siftType);
          });

        }
      },

      child: Material(
        type: MaterialType.transparency,
        borderOnForeground: true,
        child: Stack(
          children: <Widget>[
            Positioned(
              bottom: 0,
              child: GestureDetector(
                onTap: (){

                },
                child: AnimatedContainer(
                  duration: Duration(milliseconds: 200),
                  width: MediaQuery.of(context).size.width,
                  height: _widgetHeight,
                  curve: Curves.linear,
                  child: Container(
                    height: 320.px+MediaQuery.of(context).padding.bottom,
                    width: 375.px,
                    color: Colors.white,
                    child: Column(
                      children: <Widget>[

                        Expanded(
                          flex: (49.px*2).toInt(),
                          child: Container(
                            child: Row(
                              children: <Widget>[
                                SizedBox(width: 44.px,),
                                Container(
                                  width: (375-88).px,
                                  alignment: Alignment.center,
                                  child: Text(widget.titleStr,style: TextStyle(fontSize: 15.px,color: KYTextBlackColor),),
                                ),
                                GestureDetector(
                                    onTap: (){
                                      _widgetHeight = 1;
                                      setState(() {

                                      });
                                      Future.delayed(Duration(milliseconds: 200)).then((value){
                                        Navigator.of(context).pop();
                                        widget.cancelCallBack(widget.siftType);
                                      });
                                    },
                                    child: Container(
                                      width: 44.px,
                                      height: double.infinity,
                                      color: Colors.transparent,
                                      child: Image(image: AssetImage('assets/images/ky_choose_alert_close.png'),width: 24.px, height: 24.px,),
                                    )
                                )

                              ],
                            ),
                          ),
                        ),
                        Expanded(
                          flex: (1.px).toInt(),
                          child: Container(color: Color(0xFFEDEDED),),
                        ),

                        Expanded(
                          flex: ((320-52-49-0.5).px*2).toInt(),
                          child: CupertinoPicker(
                            itemExtent: 49.px,
                            diameterRatio: 0.9,
                            scrollController: FixedExtentScrollController(initialItem: _selectedIndex),
                            onSelectedItemChanged: (value){
                              LogUtils.d('selectedItemChanged=====${value}');
                              _selectedIndex = value;
                            },
                            children: widget.dataList.map((item){
                              String name = '';
                              if (item is String){
                                name = item;
                              }
                              return Container(
                                alignment: Alignment(0, 0),
                                child: Text(name,style: TextStyle(fontSize: 15.px,color: Color(0xFF1A2026)),),
                              );
                            }).toList(),
                          ),
                        ),
                        Expanded(
                          flex: (52.px*2+(MediaQuery.of(context).padding.bottom*2)).toInt(),
                          child: Container(
                            height: 52.px+MediaQuery.of(context).padding.bottom,
                            alignment: Alignment(-1, -1),
                            padding: (MediaQuery.of(context).padding.bottom > 0)?EdgeInsets.only(left: 15.px,right: 15.px,top: 8.px,bottom: MediaQuery.of(context).padding.bottom):EdgeInsets.all(0),
                            color: Colors.white,
                            child: GestureDetector(
                              onTap: (){
                                confirmWidgetClick();
                              },
                              child: Container(
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  color: widget.buttonColor,
                                  borderRadius: BorderRadius.all(Radius.circular((MediaQuery.of(context).padding.bottom > 0)?4.px:0)),
                                ),
                                child: Text('确定',style: TextStyle(color: Colors.white,fontSize: 15.px,),),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),

                  ),
                ),
              ),
            ),
          ],
        ),
      ),

    );
  }


  //确定
  void confirmWidgetClick(){
    LogUtils.d('confirmWidgetClick====');

    _widgetHeight = 1;
    setState(() {

    });
    Future.delayed(Duration(milliseconds: 200)).then((value){
      Navigator.of(context).pop();
      widget.chooseCallBack(widget.siftType,_selectedIndex);
    });

  }
}

